package com.sjs.boot.dynamic.core.run.request;


import com.sjs.boot.dynamic.core.run.MethodContext;
import com.sjs.boot.dynamic.facade.run.AbstractRequester;
import com.sjs.boot.dynamic.protocol.RetCode;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/10 15:43
 * Copyright (c) 2021 南京友拓信息技术有限公司 All rights reserved
 */
public class RestRequester extends AbstractRequester {
    @Override
    public int buildParas(MethodContext context, Map<String, Object> reqParameters) {
        HttpServletRequest req = context.getRequest();
        Map<String, String[]> p = req.getParameterMap();
        String[] v;
        int i, num;

        for (Map.Entry<String, String[]> o : p.entrySet()) {
            v = o.getValue();
            if (v == null || v.length == 0) {
                continue;
            }

            if (v.length == 1) { //not a list
                reqParameters.put(o.getKey(), v[0]);
                continue;
            }

            List<String> vv = new ArrayList<String>(); //转成list，与json保持一致
            num = v.length;
            for (i = 0; i < num; i++) {
                vv.add(v[i]);
            }
            reqParameters.put(o.getKey(), vv);
        }
        return RetCode.OK.getCode();
    }

    @Override
    public boolean init() {
        return true;
    }

    @Override
    public void destroy() {
    }
}
